﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Entity;
using Runda.CMS.Common.Utility;
using System.Collections.Generic;
using Telerik.Web.UI;
using System.Text;

namespace Runda.CMS.Portal.SalesManage
{
    public partial class RevenueClose : BasePage
    {
        public string SortString
        {
            get
            {
                if (ViewState["SortString"] == null)
                {
                    ViewState["SortString"] = "customername";
                }

                return ViewState["SortString"].ToString();
            }
            set { ViewState["SortString"] = value; }
        }

        

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                BindGrid();
            }
        }
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void gridAccessoryList_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                Label lblsettle = e.Item.FindControl("lblsettle") as Label;
                Label lblClose = e.Item.FindControl("lblClose") as Label;
                Label lblStatus1 = e.Item.FindControl("lblStatus1") as Label;
                Label lblStatus2 = e.Item.FindControl("lblStatus2") as Label;
                Label lblStatus3 = e.Item.FindControl("lblStatus3") as Label;
                Label lblisClose = e.Item.FindControl("lblisClose") as Label;

                LinkButton lnkbtn1 = e.Item.FindControl("lnkbtn1") as LinkButton;
                LinkButton lnkbtn2 = e.Item.FindControl("lnkbtn2") as LinkButton;
                LinkButton lnkbtn3 = e.Item.FindControl("lnkbtn3") as LinkButton;
                LinkButton lnkbtn4 = e.Item.FindControl("lnkbtn4") as LinkButton;
                LinkButton lnkClose = e.Item.FindControl("lnkClose") as LinkButton;
                LinkButton lnkbtnCancelClose = e.Item.FindControl("lnkbtnCancelClose") as LinkButton;

                Label lblSaleAmount = e.Item.FindControl("lblSaleAmount") as Label;
                Label lblCost = e.Item.FindControl("lblCost") as Label;
                Label lblz1Amount1 = e.Item.FindControl("lblz1Amount1") as Label;
                Label lblz1Amount2 = e.Item.FindControl("lblz1Amount2") as Label;
                Label lblz2Amount1 = e.Item.FindControl("lblz2Amount1") as Label;
                Label lblz2Amount2 = e.Item.FindControl("lblz2Amount2") as Label;
                Label lblz3Amount1 = e.Item.FindControl("lblz3Amount1") as Label;
                Label lblz3Amount2 = e.Item.FindControl("lblz3Amount2") as Label;
                Label lbli1Amount1 = e.Item.FindControl("lbli1Amount1") as Label;
                Label lbli1Amount2 = e.Item.FindControl("lbli1Amount2") as Label;
                Label lbli2Amount1 = e.Item.FindControl("lbli2Amount1") as Label;
                Label lbli2Amount2 = e.Item.FindControl("lbli2Amount2") as Label;
                Label lbli3Amount1 = e.Item.FindControl("lbli3Amount1") as Label;
                Label lbli3Amount2 = e.Item.FindControl("lbli3Amount2") as Label;
                Label lbli4Amount1 = e.Item.FindControl("lbli4Amount1") as Label;
                Label lbli4Amount2 = e.Item.FindControl("lbli4Amount2") as Label;
                Label lbli5Amount1 = e.Item.FindControl("lbli5Amount1") as Label;
                Label lbli5Amount2 = e.Item.FindControl("lbli5Amount2") as Label;

                if (lblSaleAmount.Text != lblz1Amount1.Text)
                { lblz1Amount1.ForeColor = System.Drawing.Color.Red; }
                if (lblCost.Text != lblz1Amount2.Text)
                { lblz1Amount2.ForeColor = System.Drawing.Color.Red; }

                if (lblSaleAmount.Text != lblz2Amount1.Text)
                { lblz2Amount1.ForeColor = System.Drawing.Color.Red; }
                if (lblCost.Text != lblz2Amount2.Text)
                { lblz2Amount2.ForeColor = System.Drawing.Color.Red; }

                if (lblSaleAmount.Text != lblz3Amount1.Text)
                { lblz3Amount1.ForeColor = System.Drawing.Color.Red; }
                if (lblCost.Text != lblz3Amount2.Text)
                { lblz3Amount2.ForeColor = System.Drawing.Color.Red; }

                if (lblSaleAmount.Text != lbli1Amount1.Text)
                { lbli1Amount1.ForeColor = System.Drawing.Color.Red; }
                if (lblCost.Text != lbli1Amount2.Text)
                { lbli1Amount2.ForeColor = System.Drawing.Color.Red; }

                if (lblSaleAmount.Text != lbli2Amount1.Text)
                { lbli2Amount1.ForeColor = System.Drawing.Color.Red; }
                if (lblCost.Text != lbli2Amount2.Text)
                { lbli2Amount2.ForeColor = System.Drawing.Color.Red; }

                if (lblSaleAmount.Text != lbli3Amount1.Text)
                { lbli3Amount1.ForeColor = System.Drawing.Color.Red; }
                if (lblCost.Text != lbli3Amount2.Text)
                { lbli3Amount2.ForeColor = System.Drawing.Color.Red; }

                if (lblSaleAmount.Text != lbli4Amount1.Text)
                { lbli4Amount1.ForeColor = System.Drawing.Color.Red; }
                if (lblCost.Text != lbli4Amount2.Text)
                { lbli4Amount2.ForeColor = System.Drawing.Color.Red; }

                if (lblSaleAmount.Text != lbli5Amount1.Text)
                { lbli5Amount1.ForeColor = System.Drawing.Color.Red; }
                if (lblCost.Text != lbli5Amount2.Text)
                { lbli5Amount2.ForeColor = System.Drawing.Color.Red; }

                lblClose.Text = "";
                lnkbtn2.Enabled = false;
                lnkbtn3.Enabled = false;
                lnkbtn4.Enabled = false;
                lnkClose.Visible = false;
                lnkbtnCancelClose.Visible = false;
                if (lblStatus1.Text == "0" && lblsettle.Text == "1") { lnkbtn2.Enabled = true; }
                if (lblStatus2.Text == "0" && lblStatus1.Text == "1") { lnkbtn3.Enabled = true; }
                if (lblStatus3.Text == "0" && lblStatus1.Text == "1") { lnkbtn4.Enabled = true; }
                if (lblisClose.Text == "0" && lblStatus1.Text == "1" && lblStatus2.Text == "1" && lblStatus3.Text == "1") { lnkClose.Visible = true; }
                if (lblisClose.Text == "1") { lnkbtn1.Enabled = false; lnkbtnCancelClose.Visible = true; lblClose.Text = "⊙"; }
                
                decimal SaleAmount0, Cost0, SaleAmount1, Cost1, SaleAmount, Cost;

                Label lblSaleAmount0 = e.Item.FindControl("lblSaleAmount0") as Label;
                Label lblCost0 = e.Item.FindControl("lblCost0") as Label;
                

                Label lblSign = e.Item.FindControl("lblSign") as Label;
                TextBox txtSaleAmount = e.Item.FindControl("txtSaleAmount") as TextBox;
                TextBox txtCost = e.Item.FindControl("txtCost") as TextBox;
                SaleAmount = Convert.ToDecimal(lblSaleAmount.Text);
                Cost = Convert.ToDecimal(lblCost.Text);
                SaleAmount1 = Convert.ToDecimal(txtSaleAmount.Text);
                Cost1 = Convert.ToDecimal(txtCost.Text);
                SaleAmount0 = Convert.ToDecimal(lblSaleAmount0.Text);
                Cost0 = Convert.ToDecimal(lblCost0.Text);


                if (Math.Abs(SaleAmount - SaleAmount1) < 1 && Math.Abs(Cost - Cost1)<1)
                {
                    lblSign.Text = "＝";
                }
                else
                {
                    lblSign.Text = "≠";
                }

                DataRowView dv = e.Item.DataItem as DataRowView;
                lnkbtn2.Attributes.Add("onclick", "openMyFullWin('/SalesManage/apportion2.aspx?iYear=" + dv["iYear"].ToString() + "&iMonth=" + dv["iMonth"].ToString() + "', 'apportion', 500, 700);");
                lnkbtn3.Attributes.Add("onclick", "openMyFullWin('/SalesManage/apportion3.aspx?iYear=" + dv["iYear"].ToString() + "&iMonth=" + dv["iMonth"].ToString() + "&CostDiff=" + (Cost1 - Cost0).ToString() + "&RevenueDiff=" + (SaleAmount1 - SaleAmount0).ToString() + "', 'apportion', 500, 700);");
                lnkbtn4.Attributes.Add("onclick", "openMyFullWin('/SalesManage/apportion4.aspx?iYear=" + dv["iYear"].ToString() + "&iMonth=" + dv["iMonth"].ToString() + "', 'apportion', 500, 700);");
                lnkbtnCancelClose.OnClientClick = "return OnDelConfirm();";
            }
        }

        protected void gridAccessoryList_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            Label lblMonth = e.Item.FindControl("lblMonth") as Label;
            Label lblSaleAmount = e.Item.FindControl("lblSaleAmount") as Label;
            Label lblCost = e.Item.FindControl("lblCost") as Label;
            Label lblSign = e.Item.FindControl("lblSign") as Label;
           

            int iYear, iMonth;
            decimal SaleAmount1, Cost1, SaleAmount, Cost;
            iYear = Convert.ToInt16(ddlBudgetYear.SelectedValue);
            iMonth = Convert.ToInt16(lblMonth.Text);
            SaleAmount = Convert.ToDecimal(lblSaleAmount.Text);
            Cost = Convert.ToDecimal(lblCost.Text);
            
            if (e.CommandName.ToLower() == "refresh")
            {
                TextBox txtSaleAmount = e.Item.FindControl("txtSaleAmount") as TextBox;
                TextBox txtCost = e.Item.FindControl("txtCost") as TextBox;
                SaleAmount1 = Convert.ToDecimal(txtSaleAmount.Text);
                Cost1 = Convert.ToDecimal(txtCost.Text);

                BillBll bll = new BillBll();
                string strMsg = bll.CheckBalance(new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CheckID"].ToString()), SaleAmount1, Cost1);

            }

            if (e.CommandName.ToLower() == "settle")
            {
                BillBll bll = new BillBll();
                if (bll.settleAccount(iYear, iMonth) != null)
                {
                    new BillBll().updatesettleStatus(iYear, iMonth, "1");
                }
            }

            if (e.CommandName.ToLower() == "closeaccount")
            {
                BillBll bll = new BillBll();
                bll.updateCheckStatus(iYear, iMonth, "1");
                
            }

            if (e.CommandName.ToLower() == "cancelclose")
            {
                BillBll bll = new BillBll();
                bll.updateCheckStatus(iYear, iMonth, "0");
                bll.updateApportion1Status(iYear, iMonth, "0");
                bll.updateApportion2Status(iYear, iMonth, "0");
                bll.updateApportion3Status(iYear, iMonth, "0");
                bll.updatesettleStatus(iYear, iMonth, "0");
                bll.cancelApportion(iYear, iMonth);
             
            }
            if (e.CommandName.ToLower() == "checkdata")
            {
                BillBll bll = new BillBll();
                bll.checkRptData(iYear, iMonth);
            }


            //if (e.CommandName.ToLower() == "apportion1")
            //{
            //    TextBox txtSaleAmount = e.Item.FindControl("txtSaleAmount") as TextBox;
            //    TextBox txtCost = e.Item.FindControl("txtCost") as TextBox;
            //    SaleAmount1 = Convert.ToDecimal(txtSaleAmount.Text);
            //    Cost1 = Convert.ToDecimal(txtCost.Text);
            //    BillBll bll = new BillBll();
            //    //new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CloseGUID"].ToString())
            //    bll.UpdateRptData(new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CheckID"].ToString()), iYear, iMonth, SaleAmount1, Cost1, SaleAmount, Cost);

            //}
            //if (e.CommandName.ToLower() == "apportion2")
            //{

            //    //BudgetBll bll = new BudgetBll();

            //    //bll.cancelSaleDataAdjustStatus(new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CloseGUID"].ToString()));

            //}
            //if (e.CommandName.ToLower() == "cancelclose")
            //{

            //    //BudgetBll bll = new BudgetBll();

            //    //bll.cancelDataCloseStatus(new Guid(e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["CloseGUID"].ToString()));

            //}
            BindGrid();
        }


        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid();
        }

        protected void btnRefersh_Click(object sender, EventArgs e)
        {
            BindGrid();
        } 

        private void BindGrid()
        {
            string sb = getWhereClause();

            DataTable dt = new BillBll().getRevenueCloseList(sb);

            gridAccessoryList.DataSource = dt;
            gridAccessoryList.DataBind();
            Calculate();
        }

        private void Calculate()
        {
            string sb = getWhereClause();
            BillBll bll = new BillBll();
            DataTable dt = bll.CalcRevenue(sb);

            if (dt != null && dt.Rows.Count > 0)
            {
                hfsumm01.Value = string.Format("{0:N}", dt.Rows[0]["SaleAmount"]);
                hfsumm02.Value = string.Format("{0:N}", dt.Rows[0]["Cost"]);
                hfsumm03.Value = string.Format("{0:N}", dt.Rows[0]["Revenue"]);
                hfsumm04.Value = string.Format("{0:P}", dt.Rows[0]["RevenueRate"]);
                hfsumm05.Value = string.Format("{0:N}", dt.Rows[0]["RevenueByManual"]);
                hfsumm06.Value = string.Format("{0:N}", dt.Rows[0]["CostByManual"]); ;
            }     
            
        }

        protected void gridAccessoryList_SortCommand(object source, Telerik.WebControls.GridSortCommandEventArgs e)
        {
            string OrderBy = e.SortExpression + " " + (e.NewSortOrder == Telerik.WebControls.GridSortOrder.Ascending ? "asc" : "desc");
            SortString = OrderBy;
            BindGrid();
        }

        private string getWhereClause()
        { 
            StringBuilder sb = new StringBuilder();

            
            if (ddlBudgetYear.SelectedValue != string.Empty)
            {
                sb.Append(" and iYear = ").Append(ddlBudgetYear.SelectedValue);
            }

            return sb.ToString();
        }

        
    }
}
